package com.example.redis.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.redis.pojo.Car;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author xxg
 */
@Service
@Slf4j
public class RedisTypeService {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;


    public  void hashTest(){
        Car car = new Car();
        car.setId("dd");
        car.setColor("blue");
        car.setCNo("J20j123");
        StringUtils.equalsAny("J20j123", "","");
        Map<String, Object> stringObjectMap = (Map<String, Object>) JSON.toJSON(car);
        stringRedisTemplate.opsForHash().putAll(car.getId(),stringObjectMap);
        Map<Object, Object> entries = stringRedisTemplate.opsForHash().entries(car.getId());
        System.out.println(entries);
        Set<Object> keys = stringRedisTemplate.opsForHash().keys(car.getId());
        System.out.println(keys);
        Long size = stringRedisTemplate.opsForHash().size(car.getId());
        System.out.println(size);
        List<Object> values = stringRedisTemplate.opsForHash().values(car.getId());
        System.out.println(values);
        List<Object> objects = stringRedisTemplate.opsForHash().multiGet(car.getId(), keys);
        System.out.println(objects);

    }


    public  void setTest(){
        stringRedisTemplate.opsForList().remove("key",0,"value1");
        stringRedisTemplate.opsForList().remove("key",0,"value2");
        stringRedisTemplate.opsForList().remove("key",0,"value3");
        stringRedisTemplate.opsForList().leftPush("key","value1");
        stringRedisTemplate.opsForList().leftPush("key","value2");
        stringRedisTemplate.opsForList().leftPush("key","value3");


        List<String> key1 = stringRedisTemplate.opsForList().range("key", 0, 10);
        System.out.println(key1);
        stringRedisTemplate.opsForList().trim("key",0,1);
        List<String> key5 = stringRedisTemplate.opsForList().range("key", 0, 10);
        System.out.println(key5);

    }

}
